﻿using BadmintonBuddy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BadmintonBuddy.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        [AllowAnonymous]
        [HttpPost]
        public ActionResult UserDetails(UserDetailModel model, string returnUrl)
        {
            try
            {
                if (!String.IsNullOrEmpty(model.UserId))   //If the model is having a userId associated with it then only process
                {
                    using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
                    {
                        UserDetail userDetail = Common.GetUserDetailFromUserId(Int32.Parse(model.UserId));
                        if (userDetail == null)
                        {
                            userDetail = new UserDetail();
                            userDetail.UserID = Int32.Parse(model.UserId);
                            userDetail.SkillLevel = model.Level.GetHashCode();
                        }
                        userDetail.City = Common.GetCity(model.CityName, model.StateName, model.CountryName);

                        dbContext.UserDetails.Add(userDetail);
                        dbContext.SaveChanges();

                    } //using of dbContext
                }

                if (String.IsNullOrEmpty(returnUrl))
                {
                    return View("Profile", Common.GetUserFromUserId(Int32.Parse(model.UserId)));
                }
                else
                {
                    return Redirect(returnUrl);
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dEx)
            {
                throw dEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UserClub(int clubID, int status)
        {
            if (clubID != 0)
            {
                using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
                {
                    ClubMember clubMember = null;
                    int userID = Int32.Parse(Request.Cookies["UserID"].Value);

                    IQueryable<ClubMember> clubMemberColl = from db_clubmember in dbContext.ClubMembers
                                                            where db_clubmember.UserID == userID && db_clubmember.ClubID == clubID
                                                            select db_clubmember;
                    if (clubMemberColl.Count() > 0)
                    { // If current user already exists for the same club just change his status update
                        clubMember = clubMemberColl.First<ClubMember>();
                        clubMember.PlayStatus = status;
                    }
                    else
                    {
                        clubMember = new ClubMember();
                        clubMember.UserID = userID;
                        clubMember.ClubID = clubID;
                        clubMember.PlayStatus = status;
                        dbContext.ClubMembers.Add(clubMember);
                    }
                    
                    dbContext.SaveChanges();
                }
            }
            if (status == 3)   //show users other clubs in the city and ask they want to look for partners at other locations
            {
                using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
                {
                    IQueryable<City> cityColl = from db_club in dbContext.Clubs
                                                where db_club.ClubID == clubID
                                                select db_club.City;
                    City clubCity = cityColl.First<City>();
                    RedirectToAction("CityClubs", "Club", new { ClubCity = clubCity });

                }
                
            }
        }        

    }
}
